package edu.udlap.spring2008.is300.finalexam.framework.mvc.controller;

import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.database.objects.ImageRecord;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.database.filters.ImageRecordFilter;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.Model;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.observerobservable.favorites.FavoritesChangedListener;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.observerobservable.favorites.FavoritesChangedObservable;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.observerobservable.imageset.ImageSetChangedListener;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.observerobservable.imageset.ImageSetChangedObservable;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.observerobservable.selection.SelectionChangedListener;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.observerobservable.selection.SelectionChangedObservable;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.view.ViewComponent;

/**
 *
 * @author Ruslán Ledesma Garza
 */
public abstract class Controller implements
        ImageSetChangedListener,
        SelectionChangedListener,
        FavoritesChangedListener
{

    // <editor-fold desc="base class operations...">
    protected ViewComponent view_;
    protected Model model_;
    
    public Controller(ViewComponent view, Model model)
    {
        setView(view);
        setModel(model);
    }
    
    public void setView(ViewComponent view)
    {
        view_ = view;
    }

    public void setModel(Model model)
    {
        ///we no longer want to observe the model...
        if(model_ != null)
        {
            model_.removeImageSetChangedListener(this);
            model_.removeSelectionChangedListener(this);
            model.removeFavoritesChangedListener(this);
        }
        
        ///set reference to the model and register for updates...
        model_ = model;
        
        ///suscribe to the model events...
        if(model_ != null)
        {
            model_.addImageSetChangedListener(this);
            model_.addSelectionChangedListener(this);
            model_.addFavoritesChangedListener(this);
        }
    }

    public ViewComponent getView()
    {
        return view_;
    }
    
    public Model getModel()
    {
        return model_;
    }
    
    public void initializeView()
    {
        if(view_ != null)
            view_.imageSetChanged((Model)model_);
    }

    public void setInitialState()
    {
        if(view_ != null)
            view_.setInitialState(model_);
    }
    
    public void imageSetChanged(ImageSetChangedObservable source, Object args)
    {
        ///this code would allow us to suscribe to several models at a time
        ///and manage all model notifications
        ///(although this is not necessary in the current application
        ///context)...
        if(source instanceof Model)
            if(view_ != null)
                view_.imageSetChanged((Model)source);
    }

    public void selectionChanged(SelectionChangedObservable source, Object args)
    {
        ///this code would allow us to suscribe to several models at a time
        ///and manage all model notifications
        ///(although this is not necessary in the current application
        ///context)...
        if(source instanceof Model)
            if(view_ != null)
                view_.selectionChanged((Model)source);
    }
    
    public void favoritesChanged(FavoritesChangedObservable source, Object args)
    {
        ///this code would allow us to suscribe to several models at a time
        ///and manage all model notifications
        ///(although this is not necessary in the current application
        ///context)...
        if(source instanceof Model)
            if(view_ != null)
                view_.favoritesChanged((Model)source);
    }
    // </editor-fold>
    
    // <editor-fold desc="operations (view behavior provided) by the controller (strategy)...">    
    public abstract void setSelectedImageRecord(int id);
    public abstract void nextImageRecord();
    public abstract void previousImageRecord();
    public abstract void firstImageRecord();
    public abstract void lastImageRecord();
    
    public abstract void showAddNewDialog();
    public abstract void addImageRecord(ImageRecord imageRecord);    
    public abstract void deleteSelectedImageRecord();
    public abstract void updateImageRecord(ImageRecord imageRecord);
    public abstract void toggleFavoriteStatusOfSelectedImageRecord();
    public abstract void setImageRecordFilter(ImageRecordFilter imageRecordFilter);
    
    public abstract void setGridMode();
    public abstract void setSlideShowMode();
    public abstract void toggleGridSlideMode();
    // </editor-fold>
}
